<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="票据单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNum">
              <a-input v-model="model.billNum" placeholder="请输入票据单号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择性别" disabled/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="6">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNum">
              <a-input v-model="model.idNum" placeholder="请输入身份证号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          -->
          <a-col :span="8">
            <a-form-model-item label="监室号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgCode">
              <j-select-depart v-model="model.orgCode" multi disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="balance">
              <a-input-number v-model="model.balance" placeholder="请输入余额" style="width: 100%" disabled/>

            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="消费类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="consumption_type" placeholder="请选择消费类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="消费日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="consumptionDate">
              <j-date placeholder="请选择消费日期"  v-model="model.consumptionDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="消费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number :min="0" v-model="model.amount" placeholder="请输入消费金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactor">
              <a-input v-model="model.transactor" placeholder="请输入经办人" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  import moment from 'dayjs'

  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'FpmsYlxfForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           type: [
              { required: true, message: '请输入消费类型!'},
           ],
           amount: [
              { required: true, message: '请输入消费金额!'},
           ],
           remark: [
              { required: true, message: '请输入备注!'},
           ],
        },
        url: {
          add: "/finance/fpmsYlxf/add",
          edit: "/finance/fpmsYlxf/edit",
          queryById: "/finance/fpmsYlxf/queryById",
          rule: {
            billNum: ruleBaseURL + 'shop_order_num'
          },
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        if(!record.billNum){
          this.getBillNum(record);     
        }
        if(!record.transactor){
          record.transactor = this.$store.state.user.realname;
        }
        if(!record.consumptionDate){
          record.consumptionDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        }
        this.model = Object.assign({}, record);     
        this.visible = true; 
      },
      getBillNum(record) {
        putAction(this.url.rule.billNum, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            record.billNum = res.result;
            this.model = Object.assign({}, record);     
            this.visible = true;  
          }
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.$router.push({ path: '/finance/FpmsYlxf' });
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>